package XZK010305;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Map;

import javax.management.OperationsException;

import XZK010305.bean.Express;
import XZK010305.service.ExpressService;
import XZK010305.view.Views;

/**
 * <p>
 * 快递管理训练任务 (多线程+网络编程)
 * <p>
 * 一般性（相较于此前未引入网络编程）：
 * - 1.传输一个 flag 字符串：比如插入操作，传输 ”insert“
 * - 2.传输数据体，如插入操作，传输一个 Express 对象
 * - 3.接受返回参数，如插入操作，接受一个是否插入成功的标识
 * </p>
 *
 * @author bing.z 2021/6/17 17:34
 **/
public class ExpressClient {
    /**
     * 操作面板
     */
    private static Views views = new Views();

    /**
     * 客户端工作主方法
     *
     * @param ois
     * @param oos
     * @throws IOException
     */
    public void start(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        views.welcome();
        while (true) {
            int no = views.mainMenu();
            if (no == 0) {
                oos.writeUTF("exit");
                oos.flush();
                break;
            }
            if (no == 1) {
                // 发送登录信息到服务器进行登录校验
                oos.writeUTF("adminLogin");
                oos.flush();
                // 管理员登录
                String[] msg = views.readLoginMsg();
                oos.writeObject(msg);
                boolean check = (boolean) ois.readObject();
                if (check == false) {
                    views.loginFailed();
                    continue;
                }
                views.loginSuccess();
                adminClient(ois, oos);
            } else if (no == 2) {
                // 发送登录信息到服务器进行登录校验
                oos.writeUTF("userLogin");
                oos.flush();
                // 用户登录
                String[] msg = views.readLoginMsg();
                oos.writeObject(msg);
                // 获得返回信息
                boolean check = (boolean) ois.readObject();
                if (check == false) {
                    views.loginFailed();
                    continue;
                }
                views.loginSuccess();
                userClient(ois, oos);
            }
        }
        views.bye();
    }

    /**
     * 用户客户端
     *
     * @param ois
     * @param oos
     */
    private static void userClient(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        m:
        while (true) {
            int no = views.userMenu();
            oos.writeUTF("userOpt");
            oos.flush();
            switch (no) {
                case 0:
                    oos.writeUTF("back");
                    oos.flush();
                    break m;
                case 1: {
                    oos.writeUTF("takeExpress");
                    oos.flush();
                    int code = views.takeExpress();
                    oos.writeObject(code);
                    Express express = (Express) ois.readObject();
                    if (express != null) {
                        // 取出快递,这里简单打印
                        System.out.println("取件成功!");
                        System.out.println(express);
                    } else {
                        System.out.println("取件码有误!");
                    }
                }
                break;
                default:
                    break;
            }
        }
    }

    /**
     * 快递员客户端
     *
     * @param ois
     * @param oos
     */
    private static void adminClient(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException {
        m:
        while (true) {
            int no = views.adminMenu();
            oos.writeUTF("adminOpt");
            oos.flush();
            switch (no) {
                case 0:
                    oos.writeUTF("back");
                    oos.flush();
                    break m;
                case 1: {
                    Express express = views.insert();
                    oos.writeUTF("insert");
                    oos.flush();
                    oos.writeObject(express);
                    Object object = ois.readObject();
                    if (object instanceof Express) {
                        Express insert = (Express) object;
                        if (insert != null) {
                            views.success();
                        } else {
                            views.fail();
                        }
                    }
                }
                break;
                case 2: {
                    String number = views.delete();
                    oos.writeUTF("delete");
                    oos.flush();
                    oos.writeObject(number);
                    boolean delete = (boolean) ois.readObject();
                    if (delete) {
                        views.success();
                    } else {
                        views.fail();
                    }
                }
                break;
                case 3: {
                    String number = views.getNumberOrCompany("请输入需要更新快递的单号:");
                    oos.writeUTF("update");
                    oos.flush();
                    oos.writeObject(number);
                    Express oldExpress = (Express) ois.readObject();
                    Express updateExpress = views.update(oldExpress);
                    // 更新新的值
                    oos.writeObject(updateExpress);
                    Express update = (Express) ois.readObject();
                    // 删除旧的值
                    oos.writeObject(number);
                    if (update != null) {
                        views.success();
                    } else {
                        views.fail();
                    }
                }
                break;
                case 4: {
                    oos.writeUTF("findAll");
                    oos.flush();
                    Object object = ois.readObject();
                    if (object instanceof Map) {
                        Map<String, Express> expressMap = (Map<String, Express>) object;
                        views.showAll(expressMap);
                    } else {
                        System.out.println(object);
                    }
                }
                break;
                default:
                    break;
            }
        }
    }
}
